import java.util.LinkedList;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();
        int[] num = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = input.nextInt();
        }
        int[] min = new int[n - k + 1];
        int[] max = new int[n - k + 1];
        LinkedList<Integer> maxList = new LinkedList<>();
        LinkedList<Integer> minList = new LinkedList<>();
        int r = 0;
        while (r < k) {
            if (maxList.isEmpty()) {
                maxList.add(r);
                minList.add(r++);
            } else {
                while (!maxList.isEmpty() && num[maxList.getLast()] < num[r]) {
                    maxList.removeLast();
                }
                maxList.add(r);
                while (!minList.isEmpty() && num[minList.getLast()] > num[r]) {
                    minList.removeLast();
                }
                minList.add(r++);
            }
        }
        int p = 0;
        max[p] = num[maxList.getFirst()];
        min[p++] = num[minList.getFirst()];
        while (r < n) {
            while (!maxList.isEmpty() && num[maxList.getLast()] < num[r]) {
                maxList.removeLast();
            }
            maxList.add(r);
            while (!minList.isEmpty() && num[minList.getLast()] > num[r]) {
                minList.removeLast();
            }
            minList.add(r);

            if (maxList.getFirst() <= r - k) {
                maxList.removeFirst();
            }
            if (minList.getFirst() <= r - k) {
                minList.removeFirst();
            }
            max[p] = num[maxList.getFirst()];
            min[p++] = num[minList.getFirst()];
            r++;
        }
        for (int i = 0; i < min.length; i++) {
            System.out.print(min[i] + " ");
        }
        System.out.println();
        for (int i = 0; i < max.length; i++) {
            System.out.print(max[i] + " ");
        }
    }
}